// pages/order/order.js
var common = require('../../utils/util')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderList: [],
        loading: true,
        isUpdata: false,
        searchPrams: {
            identity: 0,
            status: null,
            page: 1,
        },
        isMore: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            baseUrl: common.baseUrl
        })
        if (options) {
            if (options.status) {
                this.setData({
                    'searchPrams.status': options.status
                })
                this.initialize()
            }
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    initialize() {
        let data = this.data.searchPrams
        let array = this.data.orderList
        common.http('app/order', 'GET', (res) => {
            if (res) {
                let page = this.data.searchPrams.page
                if (res.data.length > 0) {
                    res.data.forEach(function (item) {
                        array.push(item)
                    })
                    this.setData({
                        orderList: array,
                        'searchPrams.page': res.current_page,
                        isMore: false
                    })
                    if (page == 1) {
                        app.globalData.orderList = res.data
                        if (this.data.isUpdata) {
                            this.setData({
                                orderList: app.globalData.orderList
                            })
                            if (this.data.orderList.length > 0) {
                                common.showToast('刷新成功')
                            }
                            this.setData({
                                isUpdata: false
                            })
                        }
                    }
                } else {
                    if (page > 1) {
                        common.showToast('已经到底了')
                        this.setData({
                            isMore: false
                        })
                    }
                }
            }
            this.setData({
                loading: false
            })
        }, data)
    },

    handeToggle(e) {
        let status = e.currentTarget.dataset.status
        this.setData({
            'searchPrams.status': status,
            loading: true,
            'searchPrams.page': 1,
            orderList: [],
            isUpdata: false
        })
        this.initialize()
    },

    UpdataOrder(e) {
        let status = e.currentTarget.dataset.status
        let id = e.currentTarget.dataset.id
        let price = e.currentTarget.dataset.price
        let data = {}
        this.setData({
            loading: true,
        })
        if (status == 0) {
            data.status = 2
            common.http('free/pay/request', 'POST', (res) => {
                console.log(res)
                if (res) {
                    if (res.pay_params) {
                        let params = res.pay_params
                        params.timeStamp = params.timeStamp + ''
                        let that = this
                        wx.requestPayment({
                            timeStamp: params.timeStamp,
                            nonceStr: params.nonceStr,
                            package: params.package,
                            signType: params.signType,
                            paySign: params.paySign,
                            success(res) {
                                common.showToast('支付成功', 'success')
                                that.handleSet(id,data)
                                that.initialize()
                                that.setData({
                                    loading: false,
                                })
                            },
                            fail(res) {
                                common.showToast('支付失败')
                                that.setData({
                                    loading: false,
                                })
                            },
                        })
                    }
                }
            }, {
                type: 3,
                method: 1,
                amount: price,
                scene: 'xcx',
                order_id: id
            })
        } else {
            data.status = 9
            this.handleSet(id,data)
        }
    },

    handleSet(id,data){
        common.http('app/order/update/' + id, 'POST', (res) => {
            console.log(res)
        }, data)
    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    handleUpdata: function () {
        let page = 1
        this.setData({
            isUpdata: true,
            'searchPrams.page': page,
            orderList: app.globalData.orderList
        })
        this.initialize()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    handleMore: function () {
        let page = this.data.searchPrams.page
        this.setData({
            'searchPrams.page': page + 1,
            isMore: true
        })
        this.initialize()
    },
})